const connection = require('../app/database')

class FlieService {
    // 创建头像相关信息
    async createAvatar(filename, mimetype, size, userId) {
        const statement = `INSERT INTO avatar (filename,mimetype,size,user_id) VALUES (?,?,?,?);`
        try {
            const [result] = await connection.execute(statement, [filename, mimetype, size, userId])
            return result  // 这个不能放出去
        } catch (error) {
            console.log(error);
        }
    }

    // 通过用户id获取用户头像
    async getAvatarByUserId(userId) {
        const statement = `SELECT * FROM avatar WHERE user_id =?;`
        try {
            const [result] = await connection.execute(statement, [userId])
            return result[0]
        } catch (error) {
            console.log(error);
        }
    }

    // 创建图片文件信息
    async createFile(filename, mimetype, size, userId, momentId) {
        const statement = `INSERT INTO file (filename,mimetype,size,user_id,moment_id) VALUES (?,?,?,?,?);`
        try {
            const [result] = await connection.execute(statement, [filename, mimetype, size, userId, momentId])
            return result
        } catch (error) {
            console.log(error);
        }
    }

    // 通过用户id获取用户头像
    async getFileByFilename(filename) {
        const statement = `SELECT * FROM file WHERE filename =?;`
        try {
            const [result] = await connection.execute(statement, [filename])
            return result[0]
        } catch (error) {
            console.log(error);
        }
    }
}

module.exports = new FlieService()